package sparksql

import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{DoubleType, IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SparkSession}


object DataFrames1 {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession.builder()
      .appName(this.getClass.getSimpleName)
      .master("local[*]")
      .getOrCreate()
    val sc: SparkContext = spark.sparkContext
    val rdd: RDD[String] = sc.textFile("C:\\Users\\lynn\\Desktop\\name.txt")
    //    val df: DataFrame = spark.createDataFrame(rdd, Person.getClass)
    //    df.createTempView("v_person")
    //    spark.sql("select * from v_person where score=8.0").show()
    val rowRdd: RDD[Row] = rdd.map(line => {
      val fields: Array[String] = line.split(",")
      Row(fields(0), fields(1), fields(2).toInt, fields(3).toDouble)
    })
    //创建Schema
    val schema: StructType = StructType(
      List(
        StructField("name", StringType),
        StructField("gender", StringType),
        StructField("age", IntegerType),
        StructField("score", DoubleType)
      )
    )
    import spark.implicits._
    val df: DataFrame = spark.createDataFrame(rowRdd, schema)
    df.select("*").orderBy($"score".desc).show()
  }
}

